package com.ebelter.nb.ui.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.btcomlib.models.bluetooth.products.nb.BondDevices;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.utils.MD5Utils;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.activity_manager.ActivityManagers;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.nb.R;
import com.ebelter.nb.constants.NbConstants;
import com.ebelter.nb.model.dao.DbUtils;
import com.ebelter.nb.model.http.HttpResponse2;
import com.ebelter.nb.model.http.MyStringCallback2;
import com.ebelter.nb.model.http.request.NbNetUtils;
import com.ebelter.nb.model.http.response.Login_R;
import com.ebelter.nb.model.http.response.NbBaseResponse;

public class SetPswActivity extends NBBaseActivity {


    public static final String TAG = "SetPswActivity";


    private ImageView top_left_iv;
    private TextView title_tv;
    private TextView rightTitle_tv;
    private EditText p1;
    private EditText p2;


    String phone;
    String phoneCode;



    @Override
    protected int loadLayoutById() {
        return R.layout.activity_set_psw;
    }

    @Override
    protected void initView() {
        super.initView();
        FV();

    }

    @Override
    protected void initData() {
        super.initData();

        phone = getIntent().getStringExtra("phone");
        phoneCode = getIntent().getStringExtra("phoneCode");



    }

    private void FV() {
        top_left_iv=findViewById(R.id.top_left_iv);

        top_left_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        title_tv=findViewById(R.id.m_title_tv);
        rightTitle_tv=findViewById(R.id.m_righttitle_tv);
        ViewUtils.hideView(title_tv);
        ViewUtils.hideView(rightTitle_tv);

        p1 = findViewById(R.id.cb1_ed);
        p2 = findViewById(R.id.cb2_ed);

    }

    //完成
    public void complete(View v){
        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }

        final String psw1 = p1.getText().toString().trim();
        String psw2 = p2.getText().toString().trim();
        if (TextUtils.isEmpty(psw1)){
            ToastUtil.show("密码不能为空");
            return;
        }
        if (psw1.length()>20){
            ToastUtil.show("密码长度不能超过20位");
            return;
        }
        if (!TextUtils.equals(psw1,psw2)){
            ToastUtil.show("两次密码输入不一致");
            return;
        }



        NbNetUtils.getInstance().register(this,phone,null,null,null,null,null,
                0,null,"09",psw1, phoneCode,new HttpResponse2<NbBaseResponse>() {
                    @Override
                    public void result(boolean isRequestSuccess, String requestResultDesc, int requestResultCode,
                                       NbBaseResponse nbBaseResponse, String s) {

                        LogUtils.i(TAG,"--register = "+s);


                            if (requestResultCode==1){
                                LogUtils.i(TAG,"---注册成功");
                                ToastUtil.show("注册成功");
                                DbUtils.deleteTable(BondDevices.class);//先删除掉已经绑定的设备
                                login(phone,psw1);

                            }else if (requestResultCode==0){
                                LogUtils.i(TAG,"---验证码错误");//{"resultCode": 0,"resultMessage": "验证码错误"}
                                ToastUtil.show("验证码错误");
                            }else if (requestResultCode==102){
                                LogUtils.i(TAG,"---设备未绑定组织");
                                ToastUtil.show("设备未绑定组织");
                            }else if (requestResultCode==101){
                                LogUtils.i(TAG,"---该号码已注册");
                                ToastUtil.show("该号码已注册！");
                            }else {
                                LogUtils.i(TAG,"---注册失败");
                                ToastUtil.show("注册失败");
                            }

                    }
                });




    }

    private void login(final String phone, final String psw1) {

        LogUtils.i(TAG,"---------登录phone="+phone+"--psw="+psw1);
        NbNetUtils.getInstance().login(this, phone, MD5Utils.getMd5(psw1), new HttpResponse2<Login_R>() {
            @Override
            public void result(boolean isRequestSuccess, String requestResultDesc, int requestResultCode, Login_R login_r, String s) {

                LogUtils.i(TAG,"---login--s="+s);
                LogUtils.i(TAG,"---------登录 isRequestSuccess="+isRequestSuccess+"---requestResultDesc="+requestResultDesc+"---requestResultCode="+requestResultCode+"---login_r="+login_r);
                if (isRequestSuccess){

                    if (requestResultCode== MyStringCallback2.SUCCESS&&login_r!=null&&login_r.resultMessage!=null){
                        //登录成功
                        UserSpUtil.writeString(NbConstants.User.phone, phone);
                        UserSpUtil.writeString(NbConstants.User.wsp, MD5Utils.getMd5(psw1));
                        LogUtils.i(TAG,"登录成功");
                        saveUserData(login_r);

                        Intent addDeviceIntent = new Intent(SetPswActivity.this,AddDeviceActivity.class);
                        startActivityForResult(addDeviceIntent,1);

                       // ActivityManagers.getInstance().closeActivity(PhoneRegisterActivity.TAG);
                        //finish();

                    }else {
                        if (login_r!=null){
                            LogUtils.i(TAG,login_r.message);
                        }
                    }
//                    else if (requestResultCode== 0){
//                        LogUtils.i(TAG,"登录失败");
//                        //登录失败
//                        ToastUtil.show("登录失败");
//                    }else if (requestResultCode== 2){
//                        //账号或者密码错误
//                        LogUtils.i(TAG,"账号或者密码错误");
//                        ToastUtil.show("账号或者密码错误");
//                    }else if (requestResultCode== 3){
//                        //当前手机号未注册
//                        LogUtils.i(TAG,"当前手机号未注册");
//                        ToastUtil.show("当前手机号未注册");
//                    }else {
//                        ToastUtil.show("登录失败");
//                        LogUtils.i(TAG,"登录失败");
//                    }


                }else {
                    ToastUtil.show("登录失败");
                    LogUtils.i(TAG,"登录失败");
                }






            }
        });


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtils.i(TAG,"--onActivityResult requestCode="+requestCode+"--resultCode="+resultCode);
        if (requestCode==1){

            if (resultCode==2){
                //如果资料齐全 直接跳转到主界面
                Bundle bundle = new Bundle();
                bundle.putBoolean(MainActivity.GetBandListFromNet,false);
                bundle.putBoolean(MainActivity.GetUserInfoFromNet,false);
                startActivity(MainActivity.class,bundle);

            }else {
                Bundle bundle = new Bundle();
                bundle.putBoolean(MainActivity.GetBandListFromNet,true);
                bundle.putBoolean(MainActivity.GetUserInfoFromNet,true);
                startActivity(MainActivity.class,bundle);
            }

        }
    }

    //保存用户数据
    private void saveUserData(Login_R login_r) {
        //这里不需要判断了对象是否为空了
        UserSpUtil.writeString(NbConstants.User.authkey,login_r.resultMessage.authkey);
        UserSpUtil.writeString(NbConstants.User.phone,login_r.resultMessage.phone);
        UserSpUtil.writeString(NbConstants.User.birthday,login_r.resultMessage.birthday);//yyyy-MM-dd
        UserSpUtil.writeString(NbConstants.User.email,login_r.resultMessage.email);
        UserSpUtil.writeString(NbConstants.User.name,login_r.resultMessage.name);
        UserSpUtil.writeInt(NbConstants.User.height, NumUtils.string2Int(login_r.resultMessage.height));
        UserSpUtil.writeFloat(NbConstants.User.weight,NumUtils.string2Int(login_r.resultMessage.weight));
        UserSpUtil.writeInt(NbConstants.User.sex,NumUtils.string2Int(login_r.resultMessage.sex));
        UserSpUtil.writeInt(NbConstants.User.profession,login_r.resultMessage.profession);//0非体力劳动者 1体力劳动者 2非运动员 3运动员
        UserSpUtil.writeString(NbConstants.User.modifydate,login_r.resultMessage.modifydate);

    }

}
